package com.edu.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.entity.Articles;
import com.edu.service.impl.ArticleServiceImpl;
import com.edu.entity.Types;
import com.edu.entity.Users;
import com.google.gson.Gson;

/**
 * @author 邓红学
 * Servlet implementation class ArticleController
 */
@WebServlet("/ArticleController")
public class ArticleController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//调用业务层
		ArticleServiceImpl service=new ArticleServiceImpl(); 
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op=request.getParameter("op");
		HttpSession session = request.getSession();
		Object object = session.getAttribute("frontUser");
		if (object!=null) {
			//登录后的功能
			//添加文章
			user(op, request, response);
		}else if ("queryArticlesFor5".equals(op)) {
				//前台首页显示文章
				getFiveArticles(request,response);
		}else if ("queryArticles".equals(op)) {
			//前台首页显示 全部文章
			queryArticles(request,response);
		}else if("keyWord".equals(op)) {
			//4.普通用户对文章的模糊查询操作
			frontQueryTopLike(request,response);
			
		}else if("queryLike".equals(op)) {
			//1.获取模糊查询关键字
			String articlesLike = request.getParameter("articlesLike");
			//2.调用业务层的方法
			List<Articles> list = service.queryArticlesByLike(articlesLike);
			//3.将list传递给jsp页面，存储到request作用域
			request.setAttribute("list", list);
			//把模糊查询关键字存储到request作用域给jsp页面
			request.setAttribute("articlesLike", articlesLike);
			//4.进行页面跳转
			request.getRequestDispatcher("front/index.jsp").forward(request, response);
		}else {
			response.setContentType("text/html;charset=utf-8");
			//访问的时候发现没有登录,跳转到登陆页面去
			response.getWriter().print("<script>alert('登陆去');location.href='front/index.jsp'</script>");
			//response.sendRedirect("front/index.jsp");
		}

	}


	private void queryById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		String articles = request.getParameter("articleId");
		int articleId = Integer.parseInt(articles);
		Articles articles2 = service.queryArticlesById(articleId);
		if (null==articles2) {
			response.sendRedirect("front/404.jsp");
		} else {
			boolean flag=service.addRead(articleId);
			System.out.println(flag);
			request.setAttribute("articles2", articles2);
			request.getRequestDispatcher("front/blog-single.jsp").forward(request, response);
		}
		
	}

	private void queryArticles(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 前台首页显示精选文章
		 List<Articles> list=service.queryArticles();
		
			Gson gson = new Gson();
			String articlesjson=gson.toJson(list);
			response.getWriter().print(articlesjson);
			response.getWriter().close();
		 
	}
	private void getFiveArticles(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub1
		List<Articles> list=service.queryTopArticles();
		if (list.size()>0) {
			Gson gson = new Gson();
			String articlesjson=gson.toJson(list);
			response.getWriter().print(articlesjson);
			response.getWriter().close();
		}
	}

	private void frontQueryTopLike(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		//获取模糊查询的关键字
				String keyWord = request.getParameter("keyWord");
				//调用业务层的方法
				List<Articles> list = service.queryArticlesByLike(keyWord);
				//把list通过ajax传到前台,通过gson把list转成json格式的字符串
				Gson gson = new Gson();
				String jsonString = gson.toJson(list);
				response.getWriter().print(jsonString);
				response.getWriter().close();
	}
	
	public void user(String op,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if ("addArticle".equals(op)) {
			//获取前台传递的参数
			String articleTitle=request.getParameter("articleTitle");
			String articleContent = request.getParameter("articleContent");
			String articletype=request.getParameter("types");
			System.out.println(articletype);
			//获取session储存的用户信息
			HttpSession session = request.getSession();
			Users users=(Users)session.getAttribute("frontUser");
			System.out.println(users.getUserAccount());
			//获取对应类型的编号
			Types types=new Types(Integer.valueOf(articletype));
			System.out.println(types.getTypeId());
			//int typeid=typeId.getTypeId();
			//Types typesId=typeid;
			//调用添加方法
			Articles articles=new Articles(articleTitle,users, articleContent,types);
			//判断
			boolean flag=service.insertArticle(articles);
			if (flag) {
				response.getWriter().print("<script>location.href='front/index.jsp'</script>");
			}else {
				response.getWriter().print("<script>location.href='front/addArticle.jsp'</script>");
			}
		}else if ("gettypeName".equals(op)) {
			System.out.println("是否走到这步types");
			List<String>list=service.getTypeName();
			for (String string : list) {
				System.out.println(string);
			}
			Gson gson = new Gson();
			String jsonString = gson.toJson(list);
			response.getWriter().print(jsonString);
			response.getWriter().close();
		}else if ("queryArticlesFor5".equals(op)) {
			//前台首页显示文章
			getFiveArticles(request,response);
	    }else if ("queryArticles".equals(op)) {
			//前台首页显示 全部文章
			queryArticles(request,response);
		}else if ("queryById".equals(op)) {
			queryById(request,response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
